import re

############查找匹配########
# 1、search 和 match  查找第一个匹配
#search 是接受一个正则表达式和字符串，匹配任意位置的第一个字符串，没有找到就返回None
#match  从字符串的头开始查找，只匹配以该字符开头的字符串


##########re模块中raw的用法######
# re.search(r"\\\t","san\\\tss") 以原始字符串的方式传递给re

# ###########match对象##
# msg = "It's raining cats and dogs"
# match = re.search(r"cats", msg)
# print(match.group()) # 获取的查找的对象
# print(match.start()) # 获取匹配到的对象开始的下标
# print(match.end())   # 获取匹配到的对象结束的下标

# findall 和 finditer 匹配生成的是对象
# msg = "It's raining cats and dogs,catst,cats"
# reslut = re.findall("cats",msg) #匹配全部的内容，返回一个列表
# print(reslut)
# reslut2 = re.finditer("cats",msg) #查找并返回匹配的字符串，返回一个迭代器
# print(list(reslut2))
# for i in reslut2:
#     print(i.group())

# #### re.sub("匹配正则","替换内容", string)
# msg = "This is python"
# print(re.sub("python","PYTHON",msg))


############区间##########
# ret = re.findall(r"p[Yy]thon", 'pYthon,Python3,python')
# print(ret)
############匹配或#######
# msg = "It's raining cats and dogs"
# ret = re.search("cats|dogs",msg)
# print(ret)
# ret = re.findall("cats|dogs",msg)
# print(ret)

####占位符(.) 匹配除了\n符号的任意单个字符
# ret = re.findall("p.thon","Python pYthon python")
# print(ret)


# import IPy
# # import re
# # lis1 = ["192.168.0.11","1158"]
# # if '115' in lis1:
# #     print("yes")
# input1 = input("请输入你的网段、子网掩码：")
# ips = IPy.IP(input1)
# for ip in ips:
#     print(type(ip),ip)

# msg = "I am learning python"
# msg2 = "welcome to sanchuang"
# msg3 = "python, hello world"
#
# reg = re.compile("python")
# print(reg.findall(msg))
# print(reg.findall(msg2))
# print(reg.findall(msg3))
# while True:
#     input1 = input("请输入你要输入的数字：")
#     ret1 = re.findall(r'^\+[0-9]+|^[0-9]\d+',input1)
#     ret2 = re.findall(r'^-[1-9]|^-[1-9]',input1)
#     ret3 = re.findall(r'^[0-9]+\.[0-9]+',input1)
#     ret4 = re.findall(r'^([+-][0-9])\.\d+|^[-+][1-9]\d*\.\d+',input1)
#     if input1 == '0':
#         print("0是自然数")
#     if ret1:
#         print("是正整数")
#     if ret2:
#         print("是负整数")
#     if ret3:
#         print("是正小数")
#     if ret4:
#         print("是负数小数")
#

# ret = re.search(r"([\d]{3})-([\d]{3})-([\d]{3})", "123 456-758acvf")
# print(ret.group())
# #print(ret.group(0))
# print(ret.group(1))
# print(ret.group(2))
# print(ret.group(3))

# 分组向后引用
# re.search(r"([a-z]\d)\s+([a-z]\d)\s+\1\s+\2")

# str = "a1 a2 a2 a1"
# ret = re.search(r"(?:\w\d)\s+(\w\d)\s\1", str)
# # print(ret)
# msg = 'san chuang tong le, hunan ying yi "hello world",learn python'
# ret =re.findall(r"\w+|\"[\w ]+\"",msg)
# print(ret)

#############正则分组
# msg = 'tel:173-7572-2991'
# # ret = re.search(r'(\d{3})-(\d{4})-(\d{4})',msg)
# # print(ret.group())
# # print(ret.group(3))

# str1 = "a1 d1 d1 d1 a1 d1"
# # # print(re.search(r'([a-z][0-9]) ([a-z][0-9]) \1 \2',str1).group())
# # ret = re.search(r"(?:[a-z][0-9]) ([a-z][0-9]) \1",str1)
# # print(ret.group())

# msg = '<html>test1<div>test2fff</div><div><a>test3</a></div></html>'
# ret = re.findall(r">(\w+)</",msg)
# print(ret)

# s = "a,regular,'expgular reegular'"
# # print(re.findall(r"re(?=gular)",s))
# # print(re.findall(r"re(?!gular)",s))
# # print(re.findall(r"(?<=re)gular",s))
# # print(re.findall(r"(?<=').*(?=')|\w+",s))
import requests
req = requests.get("https://www.sanchuangedu.cn/")
ret = re.findall(r"(?<=<img src=\").*\w+\.jpg", req.text)
print(ret)
length = len(ret)
for imgurl in range(length):
    src = "https://www.sanchuangedu.cn/" + ret[imgurl]
    url = str(imgurl+1)+".jpg"
    r = requests.get(src)
    with open(url,'wb') as f:
        f.write(r.content)
        print("下载了%d张"%imgurl)